package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Category;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 品类控制层
 */
@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 添加品类分类
     * @param session
     * @param category 品类名称
     * @param parentId 品类id
     * @return
     */
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session,String category,@RequestParam(value = "parentId",defaultValue = "0") int parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }

        //校验是否为管理员
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("请登录管理员账号");
        }
        return iCategoryService.addCategory(category,parentId);
    }

    /**
     * 更新品类名
     * @param session
     * @param categoryId 品类id
     * @param categoryName 品类名
     * @return
     */
    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }

        //校验是否为管理员
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("请登录管理员账号");
        }

        //更新categoryName
        return iCategoryService.updateCategoryName(categoryId,categoryName);
    }

    /**
     * 获取指定categoryId的品类的信息
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }

        //校验是否为管理员
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("请登录管理员账号");
        }

        //查询子节点的category信息,并且不递归,保持平级
        ServerResponse<List<Category>> childrenParallelCategory = iCategoryService.getChildrenParallelCategory(categoryId);
        return childrenParallelCategory;
    }

    /**
     * 通过指定的categoryId获取本节点和子节点id
     * 查询当前节点的id和递归子节点的id
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }

        //校验是否为管理员
        ServerResponse response = iUserService.checkAdminRole(user);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage("请登录管理员账号");
        }

        return iCategoryService.selectCatgoryAndChildrenById(categoryId);
    }
}

